<?php

namespace App\Vo;

class ResultVo
{
    /**
     * @var Integer
     */
    protected $code;
    /**
     * @var String
     */
    protected $msg;

    /**
     * @var T
     */
    protected $data;


    public $codeMap = [
        "ok" => 1,
        "failed" => -1
    ];


    protected function ResultVo()
    {
    }

    private static $instance = null;

    protected static function get(): ResultVo
    {
        if (empty(self::$instance)) {
            $ovj = new ResultVo();
            self::$instance = $ovj;
        } else {
            $ovj = self::$instance;
        }
        return $ovj;
    }

    public static function code(string $code = "ok"): ResultVo
    {
        $resultVo = self::get();
        $resultVo->code = $resultVo->codeMap[$code];
        return $resultVo;
    }

    public static function msg(string $msg = ""): ResultVo
    {
        $resultVo = self::get();
        $resultVo->msg = $msg;
        return $resultVo;
    }

    public static function data($data): ResultVo
    {
        $resultVo = self::get();
        $resultVo->data = $data;
        return $resultVo;
    }

    public function getCode(): int
    {
        return $this->code;
    }

    public function getMsg(): string
    {
        return $this->msg;
    }

    /*
     * 泛型
     */
    public function getData()
    {
        return $this->data;
    }

}